package cn.wildfirechat.message;

import android.os.Parcel;
import android.text.TextUtils;

import org.json.JSONException;
import org.json.JSONObject;

import cn.wildfirechat.message.core.ContentTag;
import cn.wildfirechat.message.core.MessagePayload;
import cn.wildfirechat.message.core.PersistFlag;

import static cn.wildfirechat.message.core.MessageContentType.MESSAGE_SEND_RED_PAGE;

/**
 * 发红包
 */

@ContentTag(type = MESSAGE_SEND_RED_PAGE, flag = PersistFlag.Persist_And_Count)
public class SendRedPageMessageContent extends MessageContent {
    private String hash;
    private String amount;
    private String number;
    private String slogan;
    private String fromUserId;
    private int isstate = 0;

    public SendRedPageMessageContent() {
    }


    @Override
    public MessagePayload encode() {
        MessagePayload payload = new MessagePayload();
        try {
            JSONObject objWrite = new JSONObject();
            objWrite.put("hash", hash);
            objWrite.put("amount", amount);
            objWrite.put("number", number);
            objWrite.put("slogan", slogan);
            objWrite.put("fromUserId", fromUserId);
            objWrite.put("isstate", isstate);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("redPacket", objWrite);
            payload.content = jsonObject.toString();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return payload;
    }

    @Override
    public void decode(MessagePayload payload) {
        if (!TextUtils.isEmpty(payload.content)) {
            try {
                if (payload.content != null) {
                    JSONObject object = new JSONObject(payload.content);
                    JSONObject jsonObject = object.optJSONObject("redPacket");
                    hash = jsonObject.optString("hash");
                    amount = jsonObject.optString("amount");
                    number = jsonObject.optString("number");
                    slogan = jsonObject.optString("slogan");
                    fromUserId = jsonObject.optString("fromUserId");
                    isstate = jsonObject.optInt("isstate");
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

    protected SendRedPageMessageContent(Parcel in) {
        super(in);
        this.hash = in.readString();
        this.amount = in.readString();
        this.number = in.readString();
        this.slogan = in.readString();
        this.fromUserId = in.readString();
        this.isstate = in.readInt();
    }


    @Override
    public String digest(Message message) {
        return "[红包]" + slogan;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        super.writeToParcel(dest, flags);
        dest.writeString(this.hash);
        dest.writeString(this.amount);
        dest.writeString(this.number);
        dest.writeString(this.slogan);
        dest.writeString(this.fromUserId);
        dest.writeInt(this.isstate);

    }

    public static final Creator<SendRedPageMessageContent> CREATOR = new Creator<SendRedPageMessageContent>() {
        @Override
        public SendRedPageMessageContent createFromParcel(Parcel source) {
            return new SendRedPageMessageContent(source);
        }

        @Override
        public SendRedPageMessageContent[] newArray(int size) {
            return new SendRedPageMessageContent[size];
        }
    };

    public String getHash() {
        return hash;
    }

    public void setHash(String hash) {
        this.hash = hash;
    }

    public String getAmount() {
        return amount;
    }

    public void setAmount(String amount) {
        this.amount = amount;
    }

    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }

    public String getSlogan() {
        return slogan;
    }

    public void setSlogan(String slogan) {
        this.slogan = slogan;
    }

    public String getFromUserId() {
        return fromUserId;
    }

    public int getIsstate() {
        return isstate;
    }

    public void setIsstate(int isstate) {
        this.isstate = isstate;
    }

    public void setFromUserId(String fromUserId) {
        this.fromUserId = fromUserId;
    }
}
